package com.vmock.base.core.response;

import com.vmock.base.utils.ContextUtils;
import com.vmock.base.utils.SpringUtils;
import com.vmock.biz.entity.Response;
import com.vmock.biz.entity.Url;
import com.vmock.biz.service.IResponseService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.vmock.base.utils.OutMsgUtils.outMsg;

public class PrefixProxyStrategy extends ProxyStrategy implements IResponseStrategy {

    private IResponseService responseService = SpringUtils.getBean(IResponseService.class);

    @Override
    public IMockResponse doResponse(Url mockUrl) {

        // get req and resp form context
        HttpServletRequest request = ContextUtils.getRequest();
        HttpServletResponse response = ContextUtils.getResponse();

        if (!match(mockUrl, request)) {
            proxy(request, response, request.getRequestURI());
            return null;
        }

        Response mockResponse = responseService.selectMainResponse(mockUrl.getUrlId());
        if (mockResponse.getStatusCode() != 200) {
            errorReq(response, mockResponse);
        }

        // 生成新的uri
        String newReqURI = newUri(mockUrl, request);
        proxy(request, response, newReqURI);


        return null;
    }

    private String newUri(Url mockUrl, HttpServletRequest request) {
        Response mockResponse = responseService.selectMainResponse(mockUrl.getUrlId());
        String url = mockUrl.getUrl();
        String prefix = mockResponse.getContent();

        String requestURI = request.getRequestURI();
        Pattern pattern = Pattern.compile(url);
        Matcher matcher = pattern.matcher(requestURI);
        if (!matcher.matches()) {
            throw new RuntimeException("regex not match");
        }
        return Paths.get(prefix, requestURI.substring(matcher.end(1))).toString();
    }

    /**
     * url是否匹配配置的url
     * @param mockUrl
     * @param request
     * @return
     */
    private boolean match(Url mockUrl, HttpServletRequest request) {
        String url = mockUrl.getUrl();
        String requestURI = request.getRequestURI();

        Pattern pattern = Pattern.compile(url);
        Matcher matcher = pattern.matcher(requestURI);
        if (!matcher.matches()) {
            return false;
        }

        int index = matcher.start();
        if (index != 0) {
            return false;
        }

        return true;
    }

    public static void main(String[] args) {
        String mockUrl = "(/inner/)((?!dev).[^/])+(.)*";

        String requestURI = "/inner/kcounter/410301";

        Pattern pattern = Pattern.compile(mockUrl);
        Matcher matcher = pattern.matcher(requestURI);

        System.out.println(matcher.matches());
        System.out.println(matcher.start());
        System.out.printf("/inner/ start from %d to %d\n", matcher.start(1), matcher.end(1));

//        String newUri = "/inner/dev/" + requestURI.substring(matcher.end(1));
        String newUri = Paths.get("/inner/dev", requestURI.substring(matcher.end(1) - 1)).toString();
        System.out.println(newUri);
//        ========================================

        requestURI = "/inner/kcounter/dev/410301";
        matcher = pattern.matcher(requestURI);
        System.out.println(matcher.matches());
        System.out.println(matcher.start());
        newUri = Paths.get("/inner/dev", requestURI.substring(matcher.end(1) - 1)).toString();
        System.out.println(newUri);
//        ========================================
        requestURI = "/inner/dev/kcounter/410301";
        Matcher matcher1 = pattern.matcher(requestURI);
        System.out.println(matcher1.matches());

        //        ========================================
        newUri = Paths.get("http://e.csc.com/", requestURI.substring(matcher.end(1))).toString();
        System.out.println(newUri);
    }
}
